
###
#Load Packages and Data
###
library(lfe) 
library(stargazer)
library(plyr)

load('senate.television.rollcall.RData')

##
#Local TV DMA and Agreement With Senator
##

m1 <- felm( sen.actual.agreement  ~ state.dma.share + factor(sen.party.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall)

m2 <- felm( sen.actual.agreement  ~ state.dma.share + factor(sen.party.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment > 4))

m3 <- felm( sen.actual.agreement  ~ state.dma.share  + + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment==4))

m4 <- felm( sen.actual.agreement  ~ state.dma.share + factor(sen.party.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment < 4))

#Table 7 - Main Text
stargazer(m1,m2,m3,m4,keep=c('dma.share'),dep.var.labels=c('Senator Approval','Senator Issue Agreement'),covariate.labels=c('DMA Share'),notes=c('State-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by State-Party-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

##
#Local TV DMA and Use of Issues in Evaluation of Senator
##

m1 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall)

m2 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment > 4))

m3 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment==4))

m4 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment < 4))

#Table 6 - Main Text
stargazer(m1,m2,m3,m4,keep=c('dma.share','sen.actual.agreement'),dep.var.labels=c('Senator Approval','Senator Issue Agreement'),covariate.labels=c('DMA Share','Issue Agreement','DMA Share*Issue Agreement'),notes=c('State-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by State-Party-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Subset to No Race
###

senate.television.rollcall$no.sen.election[which(is.na(senate.television.rollcall$no.sen.election))] <- 0

m1.nr <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.running==0))

m2.nr <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment > 4 & sen.running==0))

m3.nr <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(ideological.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment==4 & sen.running==0))

m4.nr <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(sen.party.alignment < 4 & sen.running==0))

#Table C5 - Appendix
stargazer(m1.nr,m2.nr,m3.nr,m4.nr,keep=c('dma.share','sen.actual.agreement'),dep.var.labels=c('Senator Approval','Senator Issue Agreement'),covariate.labels=c('DMA Share','Issue Agreement','DMA Share*Issue Agreement'),notes=c('State-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by State-Senator-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Vote Preferences
###

senate.television.rollcall$sen.vote.intent <- as.numeric(as.character(mapvalues(x=senate.television.rollcall$sen.vote.intent,from=c(-1,0,1),to=c(0,.5,1))))
senate.television.rollcall$sen.vote.post <- as.numeric(as.character(mapvalues(x=senate.television.rollcall$sen.vote.post,from=c(-1,0,1),to=c(0,.5,1))))

sen.vote.intent <- felm( sen.vote.intent  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=senate.television.rollcall)
sen.vote.post <- felm( sen.vote.post ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(ideological.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=senate.television.rollcall)

#Table C4 - Appendix
stargazer(sen.vote.intent,sen.vote.post,keep=c('dma.share','sen.actual.agreement'),dep.var.labels=c('Vote Intent','Vote'),covariate.labels=c('DMA Share','Issue Agreement','DMA Share*Issue Agreement'),notes=c('State-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by State-Party-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

#Temporal Variation
pooled.model.sen.approval.time1 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(year %in% c(2006,2008)))
pooled.model.sen.approval.time2 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(year %in% c(2010,2012)))
pooled.model.sen.approval.time3 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(year %in% c(2014,2016)))
pooled.model.sen.approval.time4 <- felm( sen.approval  ~ state.dma.share*sen.actual.agreement + factor(sen.party.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall,subset=which(year %in% c(2018,2020)))

interaction.coefs.name <- c('(2006-2008)','(2010-2012)','(2014-2016)','(2018-2020)')
interaction.coefs.point <- c(tail(pooled.model.sen.approval.time1$coefficients,1),tail(pooled.model.sen.approval.time2$coefficients,1),tail(pooled.model.sen.approval.time3$coefficients,1),tail(pooled.model.sen.approval.time4$coefficients,1))
interaction.coefs.se <- c(tail(pooled.model.sen.approval.time1$cse,1),tail(pooled.model.sen.approval.time2$cse,1),tail(pooled.model.sen.approval.time3$cse,1),tail(pooled.model.sen.approval.time4$cse,1))

senate.temporal.estimates <- cbind.data.frame(interaction.coefs.name,interaction.coefs.point,interaction.coefs.se)
names(senate.temporal.estimates) <- c('year','point','se')
senate.temporal.estimates$lower <- senate.temporal.estimates$point - 1.96*senate.temporal.estimates$se
senate.temporal.estimates$upper <- senate.temporal.estimates$point + 1.96*senate.temporal.estimates$se
save(senate.temporal.estimates,file='senate.temporal.estimates.RData')


###
#Discretized Analysis
###

senate.television.rollcall$dma.bin <- ifelse(senate.television.rollcall$state.dma.share > .8, 1, 0)

senate.television.rollcall$agreement.bin <- NA
senate.television.rollcall$agreement.bin[which(senate.television.rollcall$sen.actual.agreement <= .33)] <- 0
senate.television.rollcall$agreement.bin[which(senate.television.rollcall$sen.actual.agreement < .75 & senate.television.rollcall$sen.actual.agreement > .33)] <- 1
senate.television.rollcall$agreement.bin[which(senate.television.rollcall$sen.actual.agreement >= .75)] <- 2

pooled.model.sen.approval.coarsened <- felm( sen.approval  ~ dma.bin*factor(agreement.bin) + factor(sen.party.alignment) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | senator.state.year | 0 | senator.state.year,data=senate.television.rollcall)

#Table C3 - Appendix
stargazer(pooled.model.sen.approval.coarsened,keep=c('dma.bin','agreement.bin'),column.labels=c("(2006-2008)"),notes=c('State-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by State-Party-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)
